﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MusicDatabase
{
    /*
     * Auteur:  Joris Molnár en Niels van Leusden
     * Datum:   31-1-2013
     * Omschrijving: Deze klasse maakt het mogelijk om nieuwe songs toe te voegen.
     *               De Genres en de reeds bestaande artiesten uit de database worden opgehaald.
     *               De Methode voor het aanmaken van een nieuwe artiest wordt hier ook daarwerkelijk aangeroepen.
     *               Vervolgens wordt de song toegevoegd aan het album.              
     * 
     */
    public partial class AddSong : Form
    {
        private string[] songData;

        public string[] SongData
        {
            get { return songData; }
        }

        public AddSong()
        {
            InitializeComponent();
            LoadArtist();
            LoadGenre();
        }

        /// <summary>
        /// Load the list of artist into the combobox
        /// </summary>
        private void LoadArtist()
        {
            cbArtist.Items.Clear();

            SQLServices sqlService = new SQLServices();

            string command = "SELECT art_name FROM artist ORDER BY art_name";

            List<string[]> content = sqlService.GetTable(command);

            for (int i = 1; i < content.Count(); i++)
            {
                cbArtist.Items.Add(Convert.ToString(content[i][0]));
            }
        }

        /// <summary>
        /// Load all genre's into the combobox
        /// </summary>
        private void LoadGenre()
        {
            SQLServices sqlService = new SQLServices();

            string command = "SELECT genre_name FROM genre ORDER BY genre_name";

            List<string[]> content = sqlService.GetTable(command);

            cbGenre.Items.Add("");
            for (int i = 1; i < content.Count(); i++)
            {
                cbGenre.Items.Add(Convert.ToString(content[i][0]));
            }
            cbGenre.SelectedItem = "";
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            #region Inputchecks
            if (tbAddSong.Text.Trim() == "")
            {
                MessageBox.Show("No song name declared", "Oops");
                return;
            }

            if (tbTrack.Text == "") tbTrack.Text = "1";

            if (cbArtist.SelectedItem == null)
            {
                MessageBox.Show("Please choose an artist");
                return;
            }

            if (tbLengthMinutes.Text == "") tbLengthMinutes.Text = "0";
            if (tbLengthSeconds.Text == "") tbLengthSeconds.Text = "00";
            if (tbYear.Text == "") tbYear.Text = "0";
            #endregion

            try
            {
                string name = tbAddSong.Text.Trim();

                int artistID = GetArtist();
                int length = Convert.ToInt32(tbLengthMinutes.Text) * 60 + Convert.ToInt32(tbLengthSeconds.Text);
                int year = Convert.ToInt32(tbYear.Text);
                int genreID = GetGenre();
                int trackNr = Convert.ToInt32(tbTrack.Text);

                songData = new string[] { name, Convert.ToString(artistID), Convert.ToString(genreID), Convert.ToString(length), Convert.ToString(year), Convert.ToString(trackNr) };

                this.DialogResult = DialogResult.OK;
            }
            catch (FormatException)
            {
                MessageBox.Show("Wrong input received");
            }
            catch (Exception ex)
            {
                MessageBox.Show("An unexpected error occured! /r/n/r/nThis is the error message: /r/n" + ex.Message);
            }
        }

        /// <summary>
        /// Return the genreID by genre name
        /// </summary>
        /// <returns></returns>
        private int GetGenre()
        {
            if (cbGenre.SelectedItem.ToString() == "")
            {
                return -1;
            }

            SQLServices sqlService = new SQLServices();
            string command = "SELECT genre_id FROM genre WHERE genre_name = '" + cbGenre.SelectedItem.ToString() + "'";
            List<string[]> genreID = sqlService.GetTable(command);

            if (genreID.Count() == 2)
            {
                return Convert.ToInt32(genreID[1][0]);
            }
            else
            {
                return -1;
            }
        }

        /// <summary>
        /// Return the artistID
        /// </summary>
        /// <returns></returns>
        private int GetArtist()
        {
            SQLServices sqlService = new SQLServices();
            string command = "SELECT art_id FROM artist WHERE art_name = '" + cbArtist.SelectedItem.ToString().Replace("'", "''") + "'";
            List<string[]> artistID = sqlService.GetTable(command);

            if (artistID.Count() == 2)
            {
                return Convert.ToInt32(artistID[1][0]);
            }
            else
            {
                return 0;
            }
        }

        private void btnAddArtist_Click(object sender, EventArgs e)
        {
            AddArtist addArtist = new AddArtist();
            addArtist.ShowDialog();
            LoadArtist();
        }
    }
}
